package com.javabase.y2022.m05.day0517.v7;

/**
 * @descriptions:
 * @author: Tom
 * @date: 2022/5/17 下午 03:41
 * @version: 1.0
 */
public class Solution {
    public static int[] netherlandFlag(int[] arr, int L, int R) {
        int less = L - 1;
        int more = R;
        int index = L;
        while (index < more) {
            if (arr[index] == arr[R]) {
                index++;
            } else if (arr[index] < arr[R]) {
                swap(arr, index++, ++less);
            } else {
                swap(arr, index, --more);
            }
        }
        swap(arr, R, more);
        return new int[]{less + 1, more};
    }

    public static int partition(int[] arr, int L, int R) {
        if(L > R){
            return -1;
        }
        if(L == R){
            return L;
        }
        int lessEqual = L - 1;
        int index = L;
        while (index < R) {
            if (arr[index] <= arr[R]) {
                swap(arr, index, ++lessEqual);
            }
            index++;
        }
        swap(arr, R, ++lessEqual);
        return lessEqual;
    }

    public static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
}
